package com.irdstudio.efp.esb.api.rest.yed.repay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.basic.framework.mq.client.producer.MQProducerMessageClient;
import com.irdstudio.efp.basic.framework.mq.constant.TagEnums;
import com.irdstudio.efp.basic.framework.mq.constant.TopicEnums;
import com.irdstudio.efp.esb.api.common.EsbRespHandlerBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceBeanCreator;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.api.rest.yed.req.ReqRepayResultYedBean;
import com.irdstudio.efp.esb.api.rest.yed.resp.RespRepayResultYedBean;
import com.irdstudio.efp.esb.common.constant.EsbSysHeadEnums;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.service.bo.req.sed.repay.ReqLoanRepayResultQryBean;
import com.irdstudio.efp.esb.service.bo.resp.sed.repay.RespLoanRepayResultQryBean;
import com.irdstudio.efp.esb.service.facade.sed.repay.LoanRepayResultQryService;
import org.apache.rocketmq.common.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 需求编号：xxxxxx<br/>
 * 问题编号：<br/>
 * 功能描述：渠道方向网贷发起还款结果查询<br/>
 * 创建时间：2020/8/3 21:19
 * @Author tiankg
 * @Version 1.0
 * @Since 1.0
 */
@RestController
@RequestMapping("/esb")
public class YedRepayResultController {
    private static Logger logger = LoggerFactory.getLogger(YedRepayResultController.class);

    @ModelAttribute("esbReqServiceBean")
    public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
        logger.info("获取ESB请求对象，校验 ESB请求系统头和请求应用头对象开始");
        EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
        return esbReqServiceBean;
    }

    @ModelAttribute("reqRepayResultYedBean")
    public ReqRepayResultYedBean getReqEsbBodyBean(HttpServletRequest request) {
        logger.info("获取还款结果查询接口请求对象，校验主动还款结果查询接口请求体对象开始");
        String esbReqBody = (String) request.getAttribute("esbReqBody");
        return JSONObject.parseObject(esbReqBody, ReqRepayResultYedBean.class);
    }
    // todo
    @PostMapping("/SERVICE_20200811_01")
    public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult br1,
                                     @Validated @ModelAttribute("reqRepayResultYedBean") ReqRepayResultYedBean reqRepayResultYedBean, BindingResult br2, Exception exception) {
        String globalSerno = esbReqServiceBean.getSysHead().getGlblSrlNo();

        TraceUtil.setTraceId(globalSerno);
        logger.info("还款查询结果接口处理开始------全局流水号【" + globalSerno + "】");
        try {
            // 字段校验
            this.checkReqBean(esbReqServiceBean,reqRepayResultYedBean);

            ReqLoanRepayResultQryBean reqLoanRepayResultQryBean  = new ReqLoanRepayResultQryBean();
            reqLoanRepayResultQryBean.setTxnGlblSrlNo(reqRepayResultYedBean.getTxnGlblSrlNo());
            reqLoanRepayResultQryBean.setTxnFlowNo(reqRepayResultYedBean.getTxnFlowNo());
            reqLoanRepayResultQryBean.setBizSrlNo(reqRepayResultYedBean.getBizSrlNo());
            RespLoanRepayResultQryBean respLoanRepayResultQryBean = null;
            respLoanRepayResultQryBean = loanRepayResultQryService.loanRepayResultQry(reqLoanRepayResultQryBean);
            return EsbRespServiceHandler.esbSuccessHandler(esbReqServiceBean,respLoanRepayResultQryBean);//返回还款结果信息
        } catch (BizException e) {
            logger.error("***********调用还款结果查询接口处理失败**************还款结果查询业务流水号【" + reqRepayResultYedBean.getBizSrlNo() + "】**************" + e.getMessage());
            return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
                    JSON.toJSONString(new EsbRespHandlerBean(e.getErrorCode(), e.getMessage())),
                    EsbSysHeadEnums.RetStEnum.FAILURE.getCode(), e.getErrorCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("***********调用还款结果查询接口处理失败**************还款结果查询业务流水号【" + reqRepayResultYedBean.getBizSrlNo() + "】**************" + e.getMessage());
            return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
                    JSON.toJSONString(new EsbRespHandlerBean(EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(), e.getMessage())),
                    EsbSysHeadEnums.RetStEnum.FAILURE.getCode(), EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(), e.getMessage());
        } finally {
            logger.info("还款结果查询接口处理结束------全局流水号【" + globalSerno + "】");
            TraceUtil.clear();
        }

    }

    /**
     * 请求报文字段校验、条件校验
     */
    private EsbRespServiceBean checkReqBean(EsbReqServiceBean esbReqServiceBean,ReqRepayResultYedBean reqRepayResultYedBean) throws BizException {
        if (StringUtil.isNullorBank(reqRepayResultYedBean.getTxnGlblSrlNo()) && StringUtil.isNullorBank(reqRepayResultYedBean.getTxnFlowNo())
                && StringUtil.isNullorBank(reqRepayResultYedBean.getBizSrlNo())){//请求流水号都为空
            return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
                    JSON.toJSONString(new EsbRespHandlerBean(EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(), "交易流水号、全局流水号、业务流水号必须传入其一")),
                    EsbSysHeadEnums.RetStEnum.FAILURE.getCode(), EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(),"交易流水号、全局流水号、业务流水号必须传入其一");
        }
        return null;
    }

    @Autowired
    @Qualifier("loanRepayResultQryService")
    private LoanRepayResultQryService loanRepayResultQryService;

}
